﻿$(document).ready(function () {
    LoadTweets();
    SetTimer();
});

function LoadTweets() {
    $.ajax({
        type: "POST",
        url: url,
        data: params,
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (data) {
            ApplyTemplate(data);
        },
        error: function (xhr, errorMsg, thrown) {
            ProcessError(xhr, errorMsg, thrown);
        }
    });
}

var doLoadTweets = true;

function SetTimer() {
    $.timer(60000, function (timer) {
        if (doLoadTweets) {
            LoadTweets();
        }
        else {
            timer.stop();
        }
    });
}

function ApplyTemplate(data) {
    $('#TweetContainer').setTemplateURL(templateName, null, { filter_data: false });
    $('#TweetContainer').processTemplate(data.LoadTweetsResult);
}

function ProcessError(xhr, errorMsg, thrown) {
    doLoadTweets = false;
    if (xhr.responseText != null && xhr.responseText != "") {
        err = jQuery.parseJSON(xhr.responseText);
    }
    else {
        err = jQuery.parseJSON(xhr.statusText);
    }

    $('#TwitterErrors').setTemplateURL(errorTemplateName, null, { filter_data: false });
    $('#TwitterErrors').processTemplate(err.Message);
}  